package com.qixin.modules.eaStandard.service.impl;

import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaStandard.entity.EaAirplanestandard;
import com.qixin.modules.eaStandard.entity.EaHotelstandard;
import com.qixin.modules.eaStandard.entity.EaHotelstandarddtl;
import com.qixin.modules.eaStandard.mapper.EaHotelstandarddtlMapper;
import com.qixin.modules.eaStandard.mapper.EaHotelstandardMapper;
import com.qixin.modules.eaStandard.service.IEaHotelstandardService;
import com.qixin.modules.rzInvoice.entity.EaHotelbookspersond;
import org.jeecg.common.util.EmptyUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 酒店住宿标准
 * @Author: qixin-boot
 * @Date:   2021-07-17
 * @Version: V1.0
 */
@Service
public class EaHotelstandardServiceImpl extends ServiceImpl<EaHotelstandardMapper, EaHotelstandard> implements IEaHotelstandardService {

	@Autowired
	private EaHotelstandardMapper eaHotelstandardMapper;
	@Autowired
	private EaHotelstandarddtlMapper eaHotelstandarddtlMapper;
	@Autowired
	private ChangeIdToNameUtil changeIdToNameUtil;

	@Override
	@Transactional
	public Map<String,Object> saveMain(EaHotelstandard eaHotelstandard, List<EaHotelstandarddtl> eaHotelstandarddtlList) {
		Map<String,Object> returnMap = new HashMap<String,Object>();

		//判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
		String relationtype = eaHotelstandard.getCompanycode();   //关系类型:1用户关联,0部门职务关联
		String personnelid = EmptyUtil.checkNull(eaHotelstandard.getPersonnelid());    //用户
		String dept = EmptyUtil.checkNull(eaHotelstandard.getDept());                  //部门
		String titleid = EmptyUtil.checkNull(eaHotelstandard.getTitleid());            //职务
		String rankid = EmptyUtil.checkNull(eaHotelstandard.getRankid());              //职级

		//查询报销控制标准
		if(relationtype.equals("1")){   //按用户关联
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("personnelid",personnelid);
			List<EaHotelstandard> eaHotelstandardList = eaHotelstandardMapper.selectByMap(params);
			if(eaHotelstandardList.size() > 0){
				returnMap.put("code","-1");
				returnMap.put("message","用户:" + changeIdToNameUtil.getNameById(personnelid) + "已经维护报销控制标准,不要重复维护!");
				return returnMap;
			}else{
				eaHotelstandardMapper.insert(eaHotelstandard);

				if(eaHotelstandarddtlList!=null && eaHotelstandarddtlList.size()>0) {
					for(EaHotelstandarddtl entity:eaHotelstandarddtlList) {
						//外键设置
						entity.setZbid(eaHotelstandard.getId());
						eaHotelstandarddtlMapper.insert(entity);
					}
				}

				returnMap.put("code",0);
				returnMap.put("message","新增成功!");
				return returnMap;
			}
		}else{  //部门职务关联
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("dept",dept);
			params.put("rankid",rankid);
//            params.put("titleid",titleid);
			List<EaHotelstandard> eaHotelstandardList = eaHotelstandardMapper.selectByMap(params);
			if(eaHotelstandardList.size() > 0){
				returnMap.put("code",-1);
				returnMap.put("message","部门/职务(" + changeIdToNameUtil.getNameByDepartId(dept) + "/" + changeIdToNameUtil.getNameByTitleId(titleid) + ")已经维护报销控制标准,不要重复维护!");
				return returnMap;
			}else{
				eaHotelstandardMapper.insert(eaHotelstandard);

				if(eaHotelstandarddtlList!=null && eaHotelstandarddtlList.size()>0) {
					for(EaHotelstandarddtl entity:eaHotelstandarddtlList) {
						//外键设置
						entity.setZbid(eaHotelstandard.getId());
						eaHotelstandarddtlMapper.insert(entity);
					}
				}

				returnMap.put("code","0");
				returnMap.put("message","新增成功!");
				return returnMap;
			}
		}
	}

	@Override
	@Transactional
	public Map<String,Object> updateMain(EaHotelstandard eaHotelstandard, List<EaHotelstandarddtl> eaHotelstandarddtlList) {
		Map<String,Object> returnMap = new HashMap<String,Object>();

		//判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
		String relationtype = eaHotelstandard.getCompanycode();   //关系类型:1用户关联,0部门职务关联
		String personnelid = EmptyUtil.checkNull(eaHotelstandard.getPersonnelid());    //用户
		String dept = EmptyUtil.checkNull(eaHotelstandard.getDept());                  //部门
		String titleid = EmptyUtil.checkNull(eaHotelstandard.getTitleid());            //职务
		String rankid = EmptyUtil.checkNull(eaHotelstandard.getRankid());              //职级

		//查询报销控制标准
		if(relationtype.equals("1")){   //按用户关联
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("personnelid",personnelid);
			List<EaHotelstandard> eaHotelstandardList = eaHotelstandardMapper.selectByMap(params);
			if(eaHotelstandardList.size() > 0){
				//查询当前标准
				EaHotelstandard eaHotelstandard1 = eaHotelstandardMapper.selectById(eaHotelstandard.getId());
				if(eaHotelstandard1.getPersonnelid().equals(personnelid)){  //如果当前标准维护的用户没变,返回成功
					eaHotelstandardMapper.updateById(eaHotelstandard);

					returnMap.put("code","0");
					returnMap.put("message","编辑成功!");
					return returnMap;
				}else{
					returnMap.put("code","-1");
					returnMap.put("message","用户:" + changeIdToNameUtil.getNameById(personnelid) + "已经维护报销控制标准,不要重复维护!");
					return returnMap;
				}
			}else{
				eaHotelstandardMapper.updateById(eaHotelstandard);

				//1.先删除子表数据
				eaHotelstandarddtlMapper.deleteByMainId(eaHotelstandard.getId());

				//2.子表数据重新插入
				if(eaHotelstandarddtlList!=null && eaHotelstandarddtlList.size()>0) {
					for(EaHotelstandarddtl entity:eaHotelstandarddtlList) {
						//外键设置
						entity.setZbid(eaHotelstandard.getId());
						eaHotelstandarddtlMapper.insert(entity);
					}
				}

				returnMap.put("code","0");
				returnMap.put("message","编辑成功!");
				return returnMap;
			}
		}else{  //部门职务关联
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("dept",dept);
			params.put("rankid",rankid);
//            params.put("titleid",titleid);
			List<EaHotelstandard> eaHotelstandardList = eaHotelstandardMapper.selectByMap(params);
			if(eaHotelstandardList.size() > 0){
				//查询当前标准
				EaHotelstandard eaHotelstandard1 = eaHotelstandardMapper.selectById(eaHotelstandard.getId());
				if(eaHotelstandard1.getDept().equals(dept) && eaHotelstandard1.getRankid().equals(rankid)){  //如果当前标准维护的(单位/职务)没变,返回成功
					eaHotelstandardMapper.updateById(eaHotelstandard);

					returnMap.put("code","0");
					returnMap.put("message","编辑成功!");
					return returnMap;
				}else{
					returnMap.put("code","-1");
					returnMap.put("message","部门/职务(" + changeIdToNameUtil.getNameByDepartId(dept) + "/" + changeIdToNameUtil.getNameByTitleId(titleid) + ")已经维护报销控制标准,不要重复维护!");
					return returnMap;
				}
			}else{
				eaHotelstandardMapper.updateById(eaHotelstandard);

				//1.先删除子表数据
				eaHotelstandarddtlMapper.deleteByMainId(eaHotelstandard.getId());

				//2.子表数据重新插入
				if(eaHotelstandarddtlList!=null && eaHotelstandarddtlList.size()>0) {
					for(EaHotelstandarddtl entity:eaHotelstandarddtlList) {
						//外键设置
						entity.setZbid(eaHotelstandard.getId());
						eaHotelstandarddtlMapper.insert(entity);
					}
				}

				returnMap.put("code","0");
				returnMap.put("message","编辑成功!");
				return returnMap;
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaHotelstandarddtlMapper.deleteByMainId(id);
		eaHotelstandardMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaHotelstandarddtlMapper.deleteByMainId(id.toString());
			eaHotelstandardMapper.deleteById(id);
		}
	}

}
